package mm.chenchen.utils;

import ch.ethz.ssh2.Connection;
import lombok.extern.slf4j.Slf4j;
import mm.chenchen.model.ThreadModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class HostEnvironmentUtils {


    public static String javaVersion(Connection connection) {
        if (ObjectUtils.isEmpty(connection)) {
            return "no connection";
        }
        String result = SshUtils.execute(connection, "source /etc/profile;java -version");
        log.info("java环境执行结果为: \n{}", result);
        String[] lines = result.split("\n");
        if (ObjectUtils.isEmpty(lines) || lines.length < 1) {
            return "无查询结果!";
        }
        String firstLine = lines[0];
        String[] tem = firstLine.split("\"");
        if (lines[0].contains("command not found")) {
            return "no version";
        }
        if (tem.length > 1) {
            return tem[1];
        } else {
            return "no version";
        }
    }

    public static List<ThreadModel> getThreads(Connection connection, String name) {
        if (ObjectUtils.isEmpty(connection)) {
            return null;
        }
        List<ThreadModel> models = new ArrayList<>();
        String result = SshUtils.execute(connection, "ps -ef|grep " + name + "| grep -v grep");
        log.info("java环境执行结果为: \n{}", result);
        String[] lines = result.split("\n");
        List<String> list = Arrays.asList(lines.clone());
        for (int i = 0; i < list.size(); i++) {
            String[] tmp = list.get(i).split("\\s+");
            ThreadModel model = new ThreadModel();

            if (tmp != null && tmp.length > 7) {
                model.setUser(tmp[0]);
                model.setPid(tmp[1]);
                String tmpName = "";
                for (int x = 7; x < tmp.length; x++) {
                    tmpName = tmpName + tmp[x] + " ";
                }
                model.setThreadName(tmpName);
                models.add(model);
            }
        }
        return models;
    }

    public static String python3Version(Connection connection) {
        if (ObjectUtils.isEmpty(connection)) {
            return "no connection";
        }
        String result = SshUtils.execute(connection, "source /etc/profile;python3 --version");
        log.info("java环境执行结果为: \n{}", result);
        String[] lines = result.split("\n");
        if (ObjectUtils.isEmpty(lines) || lines.length < 1) {
            return "无查询结果!";
        }
        if (lines[0].contains("command not found")) {
            return "no version";
        }
        return lines[0];
    }

    public static String nodeVersion(Connection connection) {
        if (ObjectUtils.isEmpty(connection)) {
            return "no connection";
        }
        String result = SshUtils.execute(connection, "source /etc/profile;node -v");
        log.info("java环境执行结果为: \n{}", result);
        String[] lines = result.split("\n");
        if (ObjectUtils.isEmpty(lines) || lines.length < 1) {
            return "无查询结果!";
        }
        if (lines[0].contains("command not found")) {
            return "no version";
        }
        return lines[0];
    }

}
